package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.CouponCodeDTO;
import com.quanquan.management.dto.PayNotifyDTO;
import com.quanquan.management.request.CouponRecordRequestBody;
import com.quanquan.management.request.PayNotifyRequestBody;
import com.quanquan.management.service.ParkingService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import web.response.ResultBody;

/**
 * 功能描述:
 *
 * @ClassName: Parking
 * @Author: xiaoqianh丶
 * @Date: 2019-04-14 19:03
 * @Version: V1.0
 */
@Controller
@RequestMapping("/parking")
public class ParkingController extends ManagerBaseController {

@Autowired
private ParkingService parkingService;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) String couponName,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        CouponRecordRequestBody requestBody = new CouponRecordRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatus(sta);
        requestBody.setCouponName(couponName);
        PageDTO<CouponCodeDTO> pageDTO = parkingService.listAllParkings(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "parking/list";
    }

    @RequestMapping("/export")
    public ResultBody exportData(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total,
            @RequestParam(required = false) String couponName
    ) {
        CouponRecordRequestBody requestBody = new CouponRecordRequestBody();
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setKeyword(keyword);
        requestBody.setToDate(toDate);
        requestBody.setSize(total);
        requestBody.setCouponName(couponName);
        try{
            parkingService.export(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }

    /**
     * 停车缴费记录
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param couponName
     * @param isAsc
     * @param sta
     * @param model
     * @return
     */
    @GetMapping("/parking_record")
    public String parkingRecord(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) String couponName,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        PayNotifyRequestBody requestBody = new PayNotifyRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        PageDTO<PayNotifyDTO> pageDTO = parkingService.parkingRecord(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "parking/parking_record";
    }

    /**
     * 导出停车缴费记录
     *
     * @param keyword
     * @param fromDate
     * @param toDate
     * @param total
     * @return
     */
    @RequestMapping("/export_parking_record")
    public ResultBody exportParkingRecord(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total
    ) {
        PayNotifyRequestBody requestBody = new PayNotifyRequestBody();
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setKeyword(keyword);
        requestBody.setToDate(toDate);
        requestBody.setSize(total);
        try{
            parkingService.exportParkingRecord(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }

}

